package com.note.simpleshop.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author len 时间、字符串处理类
 *
 */
public class
DateUtil {
	private static SimpleDateFormat mon = new SimpleDateFormat("yyyy-MM");
	private static SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat daySimple = new SimpleDateFormat("yyyyMMdd");
	private static SimpleDateFormat sec = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm");
	private static SimpleDateFormat all = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	private static SimpleDateFormat smooth = new SimpleDateFormat(
			"yyyyMMddHHmmss");
	private static SimpleDateFormat smoothMilli = new SimpleDateFormat(
			"yyyyMMddHHmmssSSS");

	public static String getMonth() {
		Date date = new Date();
		String d = mon.format(date);
		return d;
	}

	public static String getDate() {
		Date date = new Date();
		String d = day.format(date);
		return d;
	}

	/**
	 * 获取年月日
	 * 
	 * @return yyyyMMdd
	 * @author Gavin 2016-05-01
	 */
	public static String getDaySimple() {
		Date date = new Date();
		String d = daySimple.format(date);
		return d;
	}

	/**
	 * 获取年月日时分
	 * 
	 * @return yyyy-MM-dd HH:mm
	 * @author Gavin 2016-05-01
	 */
	public static String getSeconds() {
		Date date = new Date();
		String d = sec.format(date);
		return d;
	}

	/**
	 * 获取年月日时分秒
	 * 
	 * @return yyyy-MM-dd HH:mm:ss
	 * @author Gavin 2016-05-01
	 */
	public static String getALLTime() {
		Date date = new Date();
		String d = all.format(date);
		return d;
	}

	/**
	 * @param date
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String getALLTime(Date date) {
		String d = all.format(date);
		return d;
	}

	public static String getDate(String format) {
		SimpleDateFormat dataFormat = new SimpleDateFormat(format);
		Date date = new Date();
		String d = dataFormat.format(date);
		return d;
	}

	/**
	 * 字符串日期格式转Date类型
	 * 
	 * @param strDate
	 *            （yyyyMMddHHmmss）
	 * @return
	 * @author Gavin 2016-05-01
	 */
	public static Date getSmoothTime(String strDate) {
		java.util.Date ret = null;
		try {
			ret = smooth.parse(strDate);
		} catch (Exception e) {
			System.out.println("~~~~~字符串转换日期时发生了异常~~~~~");
		} finally {

		}
		return ret;
	}
	public static Date getDayTime(String strDate) {
		java.util.Date ret = null;
		try {
			ret = day.parse(strDate);
		} catch (Exception e) {
			System.out.println("~~~~~字符串转换日期时发生了异常~~~~~");
		} finally {

		}
		return ret;
	}
	/**
	 * 作者： xubingbing 描述：获取当前星期几 创建日期：2016年5月1日
	 * 
	 * @param dt
	 * @return
	 */
	public static String getWeekOfDate(Date dt) {
		String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}
	/**
	 * 获取年月日时分秒
	 * 
	 * @return yyyyMMddHHmmss
	 * @author Gavin 2016-05-01
	 */
	public static String getSmoothTime() {
		Date date = new Date();
		return smooth.format(date);
	}
	/**
	 * 获取年月日时分秒
	 * 
	 * @return yyyyMMddHHmmss
	 * @author Gavin 2016-05-01
	 */
	public static String getSmoothTime(Date date) {
		return smooth.format(date);
	}
	/**
	 * 获取年月日时分秒毫秒
	 * 
	 * @return yyyyMMddHHmmssSSS
	 * @author Gavin 2016-05-01
	 */
	public static String getSmoothMilliTime() {
		Date date = new Date();
		return smoothMilli.format(date);
	}
	public static String getAfterMonthALLTime(Integer month) {
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, month);
		return f.format(c.getTime());
	}
	/**
	 * 作者： xubingbing
	 * 描述：获取两个日期之间的相差的月份
	 * 创建日期：2016年9月2日
	 * @param date1
	 * @param date2
	 * @return
	 * @throws ParseException
	 */
	public static int getMonthSpace(String date1, String date2){
		int result = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		try {
			c1.setTime(sdf.parse(date1));
			c2.setTime(sdf.parse(date2));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		result = c2.get(Calendar.MONDAY) - c1.get(Calendar.MONTH);
		return result == 0 ? 1 : Math.abs(result);
	}
	public static String fomartSs(Integer timeLong)
	{
		String str="";
		Integer hh=timeLong/3600;
		if(hh<10)
		{
			str="0"+hh+":";
		}
		else
		{
			str=hh+":";
		}
		Integer mm=timeLong%3600/60;//分
		if(mm<10)
		{
			str=str+"0"+mm+":";
		}
		else
		{
			str=str+mm+":";
		}
		Integer ss=timeLong%3600%60;//分
		if(ss<10)
		{
			str=str+"0"+ss;
		}
		else
		{
			str=str+ss;
		}
		return str;
	}
	/**
	 * 作者： xubingbing
	 * 描述：传入的必须是毫秒，如果是秒的话需要乘以1000
	 * 创建日期：2017年11月18日
	 * @param time
	 * @return
	 */
	public static String formatStamp(Long time)
	{
		String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(time);
        res = simpleDateFormat.format(date);
        return res;
	}
	public static Date after(long time)
	{
		Date now=new Date();
		Date afterDate=new Date(now.getTime()+time);//30分钟后的时间
		return afterDate;
	}
	public static Date after(Integer day)
	{
		Calendar cal=Calendar.getInstance();
		cal.add(Calendar.DAY_OF_YEAR,day);
		Date date=cal.getTime();
		return date;
	}
	public static Date after(Date currentDate,Integer day)
	{
		Calendar cal=Calendar.getInstance();
		cal.setTime(currentDate);
		cal.add(Calendar.DAY_OF_YEAR,day);
		Date date=cal.getTime();
		return date;
	}
	public static Timestamp getTimestamp()
	{
		return new Timestamp(System.currentTimeMillis());
	}
	public static void main(String[] args) {
		
		System.err.println(getALLTime(after(after(5),10)));
		
}
}
